package com.orange.controller.school;

import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.dbhelp.page.Page;
import com.orange.handler.AuthItemMapService;
import com.orange.handler.AuthItemService;
import com.orange.handler.AuthProofService;
import com.orange.handler.AuthSchoolContactService;
import com.orange.handler.AuthSchoolItemMapService;
import com.orange.handler.AuthSchoolService;
import com.orange.modules.bean.ItemAllInfo;
import com.orange.modules.bean.ItemProject;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.AuthItem;
import com.orange.modules.entity.AuthProof;
import com.orange.modules.entity.AuthSchoolItemMap;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.modules.entity.school.AuthSchoolContacts;
import com.orange.util.CommonUtil;
import com.orange.util.FreeMarkerModel;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/item")
public class AuthItemController extends BaseController{
	
	private static final Logger logger = Logger.getLogger(AuthItemController.class);
	
	@Autowired
	private AuthItemService authItemService;
	
	@Autowired
	private AuthSchoolItemMapService authSchoolItemMapService;
	
	@Autowired
	private AuthProofService authProofService;
	
	@Autowired
	private AuthSchoolService authSchoolService;
	
	@Autowired
	private AuthSchoolContactService authSchoolContactService;
	
	@Autowired
	private AuthItemMapService authItemMapService;
	
	/**
	 * 分页查询一级指标，二级指标  以及二级指标下的细则
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryItem" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryItem(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String pageNumber = map.get("pageNumber");
		String pageSize = map.get("pageSize");
		try{
			Page<AuthItem> page = authItemService.queryAuthItemBuPage(Integer.valueOf(pageNumber) , Integer.valueOf(pageSize), map);
			return toResponse(page,ReturnCode.SUCCESS,"查询成功");
		}catch(Exception e){
			logger.error(e);
			return toResponse(null,ReturnCode.FAIL,"查询失败");
		}
		
		
	}
	
	/**
	 * 新增指标或细则
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertItem" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertItem(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		AuthItem ai = new AuthItem();
		String content = map.get("content");
		String fid =map.get("fid");
		String type=map.get("type");
		String number = map.get("number");
		String major = map.get("major");
		String standards = map.get("standards");
		if(content==null || StringUtils.isBlank(content))
			return toResponse(null,ReturnCode.FAIL,"内容不能为空!");
		if(type==null || StringUtils.isBlank(type))
			return toResponse(null,ReturnCode.FAIL,"类型不能为空!");
		if(number==null || StringUtils.isBlank(number))
			return toResponse(null,ReturnCode.FAIL,"序号不能为空!");
		ai.setStandards(standards);
		ai.setContent(content);
		ai.setFid(fid);
		ai.setType(type);
		ai.setNumber(number);
		ai.setMajor(major);
		ai.setCreatetime(sdf.format(new Date()));
		authItemService.insertAuthItem(ai);
		return toResponse(ai,ReturnCode.SUCCESS,"新增成功");
	}
	
	/**
	 * 删除指标或细则
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/deleteItem" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> deleteItem(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String pid = map.get("pid");
		if(pid==null || StringUtils.isBlank(pid))
			return toResponse(null,ReturnCode.FAIL,"pid不能为空");
		AuthItem ai = authItemService.queryAuthItemById(pid);
		if(ai==null)
			return toResponse(null,ReturnCode.FAIL,"该条数据不存在");
		try{
			if("0".equals(ai.getType())){ //删除一级指标
				List<AuthItem> aii =authItemService.queryAuthItemByFid(ai.getPid(),"1"); //如果是一级指标 查看一级指标下是否有二级指标数据
				if(aii.size()>0)
					return toResponse(null,ReturnCode.FAIL,"该指标下存在二级指标，无法删除!");
				else
					authItemService.deleteAuthItem(pid);
			}else if("1".equals(ai.getType())){              //删除二级指标
				authItemService.deleteAuthItem(pid);
				List<AuthItem> list = authItemService.queryAuthItemByFid(pid, "2");//查询二级指标下的所有细则删除
				if(list.size()>0){
					for(AuthItem aa : list){
						authItemService.deleteAuthItem(aa.getPid());
					}
				}
				List<AuthItem> list1 = authItemService.queryAuthItemByFid(pid, "3");//查询二级指标下的所有认证标准删除
				if(list1.size()>0){ 
					for(AuthItem aa : list1){
						authItemService.deleteAuthItem(aa.getPid());
					}
				}
			}else{ //删除细则
				authItemService.deleteAuthItem(pid);
			}
			return toResponse(pid,ReturnCode.SUCCESS,"删除成功");
		}catch(Exception e){
			logger.error(e);
			return toResponse(pid,ReturnCode.FAIL,"删除失败");
		}
		
	}
	
	/**
	 * 修改指标或细则
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/updateItem" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateItem(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		try{
			String pid = map.get("pid");
			if(pid==null || StringUtils.isBlank(pid))
				return toResponse(null,ReturnCode.FAIL,"pid不能为空");
			AuthItem ai = authItemService.queryAuthItemById(pid);
			if(ai==null){
				return toResponse(null,ReturnCode.FAIL,"无该条数据");
			}
			String number = map.get("number");
			String content = map.get("content");
			String major = map.get("major");
			String standards = map.get("standards");
			if(number==null || StringUtils.isBlank(number)){
				return toResponse(null,ReturnCode.FAIL,"修改编号不能为空");
			}
			ai.setNumber(number);
			if(content==null || StringUtils.isBlank(content)){
				return toResponse(null,ReturnCode.FAIL,"修改内容不能为空");
			}
			ai.setContent(content);
			if(major != null){
				ai.setMajor(major);
			}
			if(standards != null){
				ai.setStandards(standards);
			}
			authItemService.insertAuthItem(ai);
			return toResponse(1,ReturnCode.SUCCESS,"修改成功");
		}catch(Exception e){
			logger.error(e);
			return toResponse(null,ReturnCode.FAIL,"修改失败");
		}
		
	}
	

	/**
	 * 查询所有二级指标内容
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAllTwice" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAllTwice(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		List<ItemProject> list = new ArrayList<ItemProject>();
		try{
			String schoolId = map.get("schoolId");
			String loginType = map.get("loginType");
			String major = map.get("major");
			
			if(schoolId==null || StringUtils.isBlank(schoolId))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校Id不能为空!");
			if(loginType==null || StringUtils.isBlank(loginType))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"专业类型不能为空!");
			if(major==null ||  StringUtils.isBlank(major))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"专业不能为空!");
			List<AuthItem> ai = authItemService.queryAuthItemTwice("1",major);//查询所有的二级指标数据
			for(AuthItem a : ai){
				ItemProject ip = new ItemProject();
				ip.setContent(a.getContent());
				String pid = a.getPid();
				ip.setPid(pid);
				List<AuthItem> ll =authItemService.queryAuthItemByFid(pid,"2");//查询所有的二级指标细则
				if(ll!=null)
					ip.setListContent(ll);
				List<AuthItem> lll =authItemService.queryAuthItemByFid(pid,"3");//查询标准
				for(AuthItem l : lll){
					ip.setStandred(l.getContent());
					break;
				}
				List<AuthSchoolItemMap> llist = authSchoolItemMapService.querySchoolItemBySchoolId(schoolId, loginType, pid);
				if(llist.size()>0)
					ip.setIsWritting("0");
				else
					ip.setIsWritting("1");
				list.add(ip);
			}
			return toResponse(list,ReturnCode.SUCCESS,"查询成功!");
		}catch(Exception e){
			logger.error(e);
			return toResponse(null,ReturnCode.FAIL,"查询失败!");
		}
		
	}
	/**
	 * 查询一级级指标内容
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/getItem" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> getItem(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String pid = map.get("pid");
		if(pid==null || StringUtils.isBlank(pid)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"pid不能为空!");
		}
		AuthItem ai = authItemService.queryAuthItemById(pid);
		return toResponse(ai,ReturnCode.SUCCESS,"查询成功!");
	}
	
	/**
	 * 查询二级指标内容
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryTwiceById" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryTwiceById(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		List<ItemProject> list = new ArrayList<ItemProject>();
		String pid = map.get("pid");
		if(pid==null || StringUtils.isBlank(pid))
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"pid不能为空!");
		try{
			AuthItem ai = authItemService.queryAuthItemById(pid);
			ItemProject ip = new ItemProject();
			ip.setContent(ai.getContent());
			String ppid = ai.getPid();
			List<AuthItem> ll =authItemService.queryAuthItemByFid(ppid,"2");//查询所有的二级指标细则
			if(ll!=null)
				ip.setListContent(ll);
			List<AuthItem> lll =authItemService.queryAuthItemByFid(ppid,"3");//查询标准
			for(AuthItem l : lll){
				ip.setStandred(l.getStandards());
				break;
			}
			list.add(ip);
			return toResponse(list,ReturnCode.SUCCESS,"查询成功!");
		}catch(Exception e){
			logger.error(e);
			return toResponse(null,ReturnCode.FAIL,"查询失败!");
		}
		
	}
	
	

	/**
	 * 查询关联二级指标所有内容
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAllTwiceInfoById" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAllTwiceInfoById(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try {
			
			
			String schoolId=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==schoolId){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			String itemId=map.get("itemId");
			if(StringUtils.isBlank(itemId)){
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"二级指标id不可为空");
			}
			
			String subject=(String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);
			
			AuthItem ai = authItemService.queryAuthItemById(map.get("itemId"));
			if(ai==null)
				return toResponse(null,ReturnCode.FAIL,"不存在该二级指标!");
			
			ItemAllInfo iai = new ItemAllInfo();
			iai.setPid(itemId);
			iai.setTitle(ai.getContent());
			iai.setStandred(ai.getStandards());
			
			List<AuthItem> list = authItemService.queryTwiceItemLevelById(itemId,schoolId,subject);
			iai.setDetailItem(list);
			
			AuthSchoolItemMap asim = authSchoolItemMapService.querySchoolItemByItemId(itemId,schoolId,subject);
			if(asim!=null){
				iai.setMainReason(asim.getReason());
				iai.setSelfdesc(asim.getSelfinstr());
				iai.setSingleLevel(asim.getSinglelevel());
			}
			List<AuthProof> listP = authProofService.queryAllAuthProof(itemId,schoolId,subject);
			iai.setFileItem(listP);
			
			return toResponse(iai,ReturnCode.SUCCESS,"查询所有内容成功!");
		} catch (Exception e) {
			logger.error("查询所有内容失败!",e);
			return toResponse(null,ReturnCode.SUCCESS,"查询所有内容失败!");
		}
		
	}
	/**
	 * 查询关联二级指标所有内容
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAllTwiceInfoById1" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAllTwiceInfoById1(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try {
			
			
			String schoolId=map.get("schoolId");
			if(null==schoolId){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			String itemId=map.get("itemId");
			if(StringUtils.isBlank(itemId)){
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"二级指标id不可为空");
			}
			
			String subject=map.get("loginType");
			
			AuthItem ai = authItemService.queryAuthItemById(map.get("itemId"));
			if(ai==null)
				return toResponse(null,ReturnCode.FAIL,"不存在该二级指标!");
			
			ItemAllInfo iai = new ItemAllInfo();
			iai.setPid(itemId);
			iai.setTitle(ai.getContent());
			iai.setStandred(ai.getStandards());
			
			List<AuthItem> list = authItemService.queryTwiceItemLevelById(itemId,schoolId,subject);
			iai.setDetailItem(list);
			
			AuthSchoolItemMap asim = authSchoolItemMapService.querySchoolItemByItemId(itemId,schoolId,subject);
			if(asim!=null){
				iai.setMainReason(asim.getReason());
				iai.setSelfdesc(asim.getSelfinstr());
				iai.setSingleLevel(asim.getSinglelevel());
			}
			List<AuthProof> listP = authProofService.queryAllAuthProof(itemId,schoolId,subject);
			iai.setFileItem(listP);
			
			return toResponse(iai,ReturnCode.SUCCESS,"查询所有内容成功!");
		} catch (Exception e) {
			logger.error("查询所有内容失败!",e);
			return toResponse(null,ReturnCode.SUCCESS,"查询所有内容失败!");
		}
		
	}
	
	/**
	 * 查询所有一级指标
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAllOnesInfo" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAllOnesInfo(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		List<AuthItem> list =authItemService.queryAuthItemTwice("0");//查询所有一级指标
		return toResponse(list,ReturnCode.SUCCESS,"查询成功!");
	}
	
	
	
	/**
	 * 生成随机数
	 * @param length
	 * @return
	 */
	public static String getRandomCharAndNumr(Integer length) {  
	    String str = "";  
	    Random random = new Random();  
	    for (int i = 0; i < length; i++) {  
	       
	            str += String.valueOf(random.nextInt(10));  
	    }  
	    return str+new Date().getTime();  
	} 
	
	
	/**
	 * 根据一级指标id查询所有二级指标
	 * @param request
	 * @param pid
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryItemInfoByFid" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryItemInfoByFid(HttpServletRequest request,@RequestParam String pid) throws SQLException{
		
		try {
			String schoolId=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==schoolId){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			AuthSchool school=authSchoolService.queryAuthSchoolInfoById(schoolId);
			if(null==school){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该学校信息不存在");
			}
			AuthItem a=authItemService.queryAuthItemById(pid);
			if(null==a){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该指标信息不存在!");
			}
			List<AuthItem> list =authItemService.queryAuthItemsByFidAndMajor(a.getPid(), school.getMajor());
			a.setList(list);
			return toResponse(a,ReturnCode.SUCCESS,"根据一级指标id查询所有二级指标成功!");
		} catch (Exception e) {
			logger.error("根据一级指标id查询所有二级指标失败!",e);
			return toResponse(null,ReturnCode.SUCCESS,"根据一级指标id查询所有二级指标失败!");
		}
		
	}
	
	/**
	 * 查询未填报的二级指标
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryNotSubmit" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryNotSubmit(HttpServletRequest request) throws SQLException{
		
		try {
			String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==schoolId){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			AuthSchool school=authSchoolService.queryAuthSchoolInfoById(schoolId);
			//查询所有的一级指标
			List<AuthItem> indexs = this.authItemService.queryAuthItemIndex();
			List<String> strs = new ArrayList<String>();
			//根据一级指标查到对应的二级指标
			for(AuthItem a:indexs){
				List<AuthItem> as = this.authItemService.queryAuthItemsByFidAndMajor(a.getPid(), school.getMajor());
				//查询没填报的二级指标
				for(AuthItem s:as){
					if(s.getState()==null){
						strs.add(s.getContent());
					}
				}
			}
			//查询是否填写教科院或教务处负责人信息
			List<AuthSchoolContacts> contacts1 = this.authSchoolContactService.queryType1BySchool(schoolId);
			List<AuthSchoolContacts> contacts2 = this.authSchoolContactService.queryType2BySchool(schoolId);
			if(contacts1.size() == 0){
				strs.add("教科院联系人信息未填写");
			}
			if(contacts2.size() == 0){
				strs.add("教务处联系人信息未填写");
			}
			if(strs.size()==0){
				//修改分项填报状态
				this.authItemMapService.updateStateBySchool(schoolId,"2");
				//修改学校填报状态
				AuthSchool a=authSchoolService.queryAuthSchoolInfoById(schoolId);
				a.setState("1");
				this.authSchoolService.updateAuthSchoolInfo(a);
			}
			if(strs.size()>0){
				return toResponse(strs, ReturnCode.SUCCESS, "查询未填报成功");
			}else{
				return toResponse("ok", ReturnCode.SUCCESS, "查询未填报成功");
			}
		} catch (Exception e) {
			logger.error("根据一级指标id查询所有二级指标失败!",e);
			return toResponse(null,ReturnCode.FAIL,"根据一级指标id查询所有二级指标失败!");
		}
		
	}
	
	/**
	 * 提交认证申请
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/submit" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> submit(HttpServletRequest request) throws SQLException{
		
		try {
			String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==schoolId){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			//修改分项填报状态
			this.authItemMapService.updateStateBySchool(schoolId,"2");
			//修改学校填报状态
			AuthSchool a=authSchoolService.queryAuthSchoolInfoById(schoolId);
			a.setState("1");
			this.authSchoolService.updateAuthSchoolInfo(a);
			return toResponse(null, ReturnCode.SUCCESS, "提交成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null, ReturnCode.SUCCESS, "提交失败");
		}
		
	}
	
	
	
	
	/**
	 * 根据二级指标id查询所有细则
	 * @param request
	 * @param pid
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryTwoItemInfoByFid" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryTwoItemInfoByFid(HttpServletRequest request,@RequestParam String pid) throws SQLException{
		
		try {
			String schoolId=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==schoolId){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			AuthSchool school=authSchoolService.queryAuthSchoolInfoById(schoolId);
			if(null==school){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该学校信息不存在");
			}
			AuthItem a=authItemService.queryAuthItemById(pid);
			if(null==a){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该指标信息不存在!");
			}
			List<AuthItem> list =authItemService.queryAuthItemByFid(a.getPid(),"2");
			return toResponse(list,ReturnCode.SUCCESS,"根据二级指标id查询所有细则成功!");
		} catch (Exception e) {
			logger.error("根据二级指标id查询所有细则失败!",e);
			return toResponse(null,ReturnCode.SUCCESS,"根据二级指标id查询所有细则失败!");
		}
		
	}
	
	
	/***********************************************************************************************************************/
	/**
	 * 根据一级指标id查询所有二级指标
	 * @param request
	 * @param pid1
	 * @param schoolId
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryItemInfoByFid1" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryItemInfoByFid1(HttpServletRequest request,@RequestParam String pid1,@RequestParam String schoolId) throws SQLException{
		
		try {
			AuthSchool school=authSchoolService.queryAuthSchoolInfoById(schoolId);
			if(null==school){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该学校信息不存在");
			}
			AuthItem a=authItemService.queryAuthItemById(pid1);
			if(null==a){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该指标信息不存在!");
			}
			List<AuthItem> list =authItemService.queryAuthItemsByFidAndMajor(a.getPid(), school.getMajor());
			a.setList(list);
			return toResponse(a,ReturnCode.SUCCESS,"根据一级指标id查询所有二级指标成功!");
		} catch (Exception e) {
			logger.error("根据一级指标id查询所有二级指标失败!",e);
			return toResponse(null,ReturnCode.SUCCESS,"根据一级指标id查询所有二级指标失败!");
		}
		
	}
	
	/**
	 * 二级指标细则下载
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/downloadFile" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public void downloadFile(HttpServletRequest request,@RequestParam String pid,@RequestParam String number,HttpServletResponse response) throws SQLException{
		List<ItemProject> list = new ArrayList<ItemProject>();
		String schoolId=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		String subject=(String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			AuthItem ai = authItemService.queryAuthItemById(pid);
			ItemProject ip = new ItemProject();
			ip.setContent(ai.getContent());
			String ppid = ai.getPid();
			List<AuthItem> ll =authItemService.queryAuthItemByFid(ppid,"2");//查询所有的二级指标细则
			if(ll!=null)
				ip.setListContent(ll);
			List<AuthItem> lll =authItemService.queryAuthItemByFid(ppid,"3");//查询标准
			for(AuthItem l : lll){
				ip.setStandred(l.getStandards());
				break;
			}
			list.add(ip);
			
			//测评细则和序号
			String str = "";
			int index = 1;
			List<String> numbers = new ArrayList<String>();
			for(AuthItem i:ll){
				numbers.add(number+"."+index);
				str += number+"."+index+"、"+i.getContent()+"<w:p></w:p>";
				index++;
			}
			map.put("allcontent", str);
			
			//查询该指标下的自评等级
			List<AuthItem> lists = authItemService.queryTwiceItemLevelById(pid,schoolId,subject);
			for (int i = 0; i < lists.size(); i++) {
				lists.get(i).setNumber(numbers.get(i));
			}
			map.put("levelList", lists);
			
			AuthItem au = authItemService.queryAuthItemById(pid);
			ItemAllInfo iai = new ItemAllInfo();
			iai.setPid(pid);
			iai.setTitle(au.getContent());
			iai.setStandred(au.getStandards());
			AuthSchoolItemMap asim = authSchoolItemMapService.querySchoolItemByItemId(pid,schoolId,subject);
			if(asim!=null){
				iai.setMainReason(asim.getReason());
				iai.setSelfdesc(asim.getSelfinstr());
				iai.setSingleLevel(asim.getSinglelevel());
			}
			map.put("data", iai);
			//佐证要目信息
			List<AuthProof> listP = authProofService.queryAllAuthProof(pid,schoolId,subject);
			for(AuthProof a:listP){
				if(a.getOpen().equals("1")){
					a.setOpen("是");
				}else{
					a.setOpen("否");
				}	
			}
			map.put("proof", listP);
			map.put("content", au.getContent());
			map.put("schoolId", schoolId);
			FreeMarkerModel frm = new FreeMarkerModel();
			frm.createDoc1(request, response, map,iai.getTitle()+".doc");
		}catch(Exception e){
			logger.error(e);
		}
	}
}
